using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component.Operation
{
	public class Game6Component : OperationComponent
	{
		private float MIN_X = -2.37f;

		private float MAX_X = -0.24f;

		private float MIN_Y = -3.25f;

		private float MAX_Y = -1.39f;

		private Transform btnBack;

		private SpriteRenderer waterSprite;

		private Transform pole;

		private List<Transform> efcs = new List<Transform>();

		private float fillAmount = 1f;

		private Transform needleTubing;

		private Transform target;

		private bool isMoveComplete;

		private bool isMoving;

		private bool isTouching;

		private bool isComplete;

		private Vector2 lastTouchPoint;

		private int curColorIndex;

		private Color32[] colors = new Color32[6]
		{
			new Color32(227, 81, 82, byte.MaxValue),
			new Color32(byte.MaxValue, 151, 57, byte.MaxValue),
			new Color32(197, 176, byte.MaxValue, byte.MaxValue),
			new Color32(45, 170, 173, byte.MaxValue),
			new Color32(byte.MaxValue, 223, 87, byte.MaxValue),
			new Color32(82, 220, 180, byte.MaxValue)
		};

		private float newX;

		private float newY;

		private void Awake()
		{
			btnBack = base.transform.Find("BtnBack");
			needleTubing = base.transform.Find("NeedleTubing");
			waterSprite = needleTubing.Find("WaterFill").GetComponent<SpriteRenderer>();
			float num = waterSprite.sprite.texture.height;
			Rect textureRect = waterSprite.sprite.textureRect;
			float num2 = textureRect.yMin / num;
			float value = textureRect.yMax / num - num2;
			waterSprite.material.SetFloat("_StartY", num2);
			waterSprite.material.SetFloat("_TotalY", value);
			pole = needleTubing.Find("Pole");
			target = base.transform.Find("Target");
			target.SetLocalXY(Random.Range(MIN_X, MAX_X), Random.Range(MIN_Y, MAX_Y));
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (displayer == null)
			{
				return;
			}
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 1);
			isTouching = false;
			isMoving = false;
			if (raycastHit2D.collider != null)
			{
				if (raycastHit2D.collider.transform == btnBack)
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					displayer.ChangeType(OperationDisplayerItem.Type.Main);
				}
				else if (raycastHit2D.collider.transform == needleTubing)
				{
					if (!isMoveComplete)
					{
						isMoving = true;
					}
					else if (!isComplete)
					{
						Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
						ChangeColor();
					}
				}
				else if (raycastHit2D.collider.transform == pole && !isComplete)
				{
					isTouching = true;
				}
			}
			lastTouchPoint = touchEventData.curPos;
		}

		private void ChangeColor()
		{
			curColorIndex++;
			if (curColorIndex >= colors.Length)
			{
				curColorIndex = 0;
			}
			waterSprite.color = colors[curColorIndex];
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			if (isMoving)
			{
				newX = needleTubing.localPosition.x + touchEventData.curPos.x - lastTouchPoint.x;
				newY = needleTubing.localPosition.y + touchEventData.curPos.y - lastTouchPoint.y;
				if (newX < MIN_X)
				{
					newX = MIN_X;
				}
				else if (newX > MAX_X)
				{
					newX = MAX_X;
				}
				if (newY < MIN_Y)
				{
					newY = MIN_Y;
				}
				else if (newY > MAX_Y)
				{
					newY = MAX_Y;
				}
				if (Mathf.Abs(newX - target.localPosition.x) < 0.1f && Mathf.Abs(newY - target.localPosition.y) < 0.1f)
				{
					newX = target.localPosition.x;
					newY = target.localPosition.y;
					needleTubing.SetLocalXY(newX, newY);
					pole.GetComponent<BoxCollider2D>().enabled = true;
					isMoving = false;
					isMoveComplete = true;
					Singleton<AudioManager>.Instance.PlaySound("sound_embed");
				}
				else
				{
					needleTubing.SetLocalXY(newX, newY);
				}
			}
			else if (isTouching)
			{
				float num = touchEventData.curPos.y - lastTouchPoint.y;
				if (num < 0f)
				{
					SetFillAmount(fillAmount + num);
					if (fillAmount <= 0f)
					{
						isComplete = true;
						isTouching = false;
						Singleton<AudioManager>.Instance.PlaySound("sound_blink");
						Transform transform = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", needleTubing.transform.position + new Vector3(0f, 0f, -1E-05f), 3f);
						transform.SetParent(base.transform);
						efcs.Add(transform);
					}
				}
			}
			lastTouchPoint = touchEventData.curPos;
		}

		private void SetFillAmount(float val)
		{
			fillAmount = Mathf.Clamp(val, 0f, 1f);
			waterSprite.material.SetFloat("_Fill", fillAmount);
			pole.SetLocalY(1.625f * fillAmount + 2.33f);
		}

		private void OnDestroy()
		{
			for (int i = 0; i < efcs.Count; i++)
			{
				if (efcs[i] != null)
				{
					Singleton<GamePoolManager>.Instance.Despawn(efcs[i]);
				}
			}
			efcs.Clear();
		}
	}
}
